package findmine;

import java.util.Scanner;

public class Games {
    public static int row = 9;
    public static int column = 9;
    public static int wide = 11;
    public static int high = 11;
    public static int mineCount = 10;
    public static boolean b = true;

    public static void main(String[] args) {
        menu();
    }

    public static void menu() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("==================");
        System.out.println("===== 1.play =====");
        System.out.println("===== 0.exit =====");
        System.out.println("==================");
        do {
            System.out.println("扫雷游戏");
            System.out.println("请选择: ");
            int key = scanner.nextInt();
            switch (key) {
                case 1: {
                    System.out.println("游戏开始");
                    game();
                    break;
                }
                case 0: {
                    System.out.println("已退出");
                    b = false;
                    break;
                }
                default: {
                    System.out.println("选择有误，请重新选择");
                    break;
                }
            }
        } while (b);
    }

    public static void game() {
        char[][] c1 = new char[wide][high];
        char[][] c2 = new char[wide][high];
        show(c1, '0');
        show(c2, '*');
        setMine(c1);
        printMap(c2);
        printMap(c1);
        checkMine(c2, c1);
    }

    public static void show(char[][] c1, char c) {
        for (int i = 0; i < c1.length; i++) {
            for (int j = 0; j < c1[i].length; j++) {
                c1[i][j] = c;
            }
        }
    }

    public static void printMap(char[][] c1) {
        System.out.println("==================");
        for (int i = 0; i <= row; i++) {
            System.out.print(i + "  ");
        }
        System.out.println();
        for (int i = 1; i <= row; i++) {
            System.out.print(i + "  ");
            for (int j = 1; j <= column; j++) {
                System.out.print(c1[i][j] + "  ");
            }
            System.out.println();
        }
    }

    public static void setMine(char[][] c1) {
        int trapX, trapY;
        int nums = mineCount;
        for (int i = 0; nums > 0; i++) {
            trapX = (int) (Math.random() * 9 + 1);
            trapY = (int) (Math.random() * 9 + 1);
            if (c1[trapX][trapY] != '1') {
                c1[trapX][trapY] = '1';
                nums--;
            }
        }
    }

    public static void checkMine(char[][] c2, char[][] c1) {
        Scanner scanner = new Scanner(System.in);
        int right = 0;
        while (right < (row * column - mineCount)) {
            System.out.println("请输入要排查的位置");
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            if (c1[x][y] == '1') {
                System.out.println("你被炸死了......");
                System.out.println("地雷信息如下");
                printMap(c1);
                break;
            } else {
                c2[x][y] = (char) (getMineNum(c1, x, y) + '0');
                printMap(c2);
                right++;
            }
        }
        if(right == (row * column - mineCount)){
            System.out.println("已排查所有地雷，你获胜了！");
            System.out.println("地雷信息如下");
            printMap(c1);
            b = false;
        }
    }

    public static int getMineNum(char[][] c1, int x, int y) {
        return c1[x - 1][y] + c1[x + 1][y] + c1[x - 1][y - 1] +
                c1[x - 1][y + 1] + c1[x + 1][y - 1] + c1[x + 1][y + 1] +
                c1[x][y - 1] + c1[x][y + 1] - 8 * '0';
    }
}

